package com.googlecode.geoteacher.db;

import android.provider.BaseColumns;

public final class DatabaseContract {

	// constants to work with the database
	public static final String PRAGMA_FOREIGN_KEYS_ON = "PRAGMA foreign_keys=ON;";
	public static final int BOOLEAN_TRUE = 1;
	public static final int BOOLEAN_FALSE = 0;

	// string constants for TABLE_CREATE
	private static final String CREATE_TABLE = "CREATE TABLE ";
	private static final String DROP_TABLE_IF_EXISTS = "DROP TABLE IF EXISTS ";
	private static final String TEXT_TYPE = " TEXT";
	private static final String INTEGER_TYPE = " INTEGER";
	private static final String PRIMARY_KEY_AUTO = " PRIMARY KEY AUTOINCREMENT";
	private static final String UNIQUE = " UNIQUE";
	private static final String NOT_NULL = " NOT NULL";
	private static final String FOREIGN_KEY = "FOREIGN KEY";
	private static final String REFERENCES = "REFERENCES ";
	private static final String ON_DELETE_CASCADE = " ON DELETE CASCADE";
	private static final String COMMA_SEP = ",";
	private static final String OPEN_BRACKET = "(";
	private static final String CLOSING_BRACKET = ")";
	private static final String END = ";";

	/**
	 * Current version of the database.
	 */
	public static final int DATABASE_VERSION = 1;

	/**
	 * Name of the database.
	 */
	public static final String DATABASE_NAME = "game.db";

	private DatabaseContract() {
	}

	public static abstract class ChallengeResultEntry implements BaseColumns {
		public static final String TABLE_NAME = "challenge_result";
		public static final String COLUMN_CHALLENGE_ID = "challenge_id";
		public static final String COLUMN_DISTANCE = "distance";
		public static final String COLUMN_CREATED = "created";

		public static final String[] ALL_COLUMNS = { _ID, COLUMN_CHALLENGE_ID,
				COLUMN_DISTANCE, COLUMN_CREATED };

		public static final String TABLE_CREATE = CREATE_TABLE + TABLE_NAME
				+ OPEN_BRACKET + _ID + INTEGER_TYPE + PRIMARY_KEY_AUTO
				+ COMMA_SEP + COLUMN_CHALLENGE_ID + INTEGER_TYPE + NOT_NULL
				+ COMMA_SEP + COLUMN_DISTANCE + INTEGER_TYPE + NOT_NULL
				+ COMMA_SEP + COLUMN_CREATED + INTEGER_TYPE + NOT_NULL
				+ COMMA_SEP + FOREIGN_KEY + OPEN_BRACKET + COLUMN_CHALLENGE_ID
				+ CLOSING_BRACKET + REFERENCES
				+ DatabaseContract.ChallengeEntry.TABLE_NAME + OPEN_BRACKET
				+ DatabaseContract.ChallengeEntry._ID + CLOSING_BRACKET
				+ ON_DELETE_CASCADE + CLOSING_BRACKET + END;

		public static final String TABLE_DROP = DROP_TABLE_IF_EXISTS
				+ TABLE_NAME + END;
	}

	public static abstract class ChallengeEntry implements BaseColumns {
		public static final String TABLE_NAME = "challenge";
		public static final String COLUMN_HASH = "hash";
		public static final String COLUMN_NAME = "name";
		public static final String COLUMN_PATH = "path";

		public static final String[] ALL_COLUMNS = { _ID, COLUMN_HASH,
				COLUMN_NAME, COLUMN_PATH };

		public static final String TABLE_CREATE = CREATE_TABLE + TABLE_NAME
				+ OPEN_BRACKET + _ID + INTEGER_TYPE + PRIMARY_KEY_AUTO
				+ COMMA_SEP + COLUMN_HASH + TEXT_TYPE + UNIQUE + NOT_NULL
				+ COMMA_SEP + COLUMN_NAME + TEXT_TYPE + NOT_NULL + COMMA_SEP
				+ COLUMN_PATH + TEXT_TYPE + NOT_NULL + CLOSING_BRACKET + END;

		public static final String TABLE_DROP = DROP_TABLE_IF_EXISTS
				+ TABLE_NAME + END;
	}
}
